<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="机台" prop="machineId">
        <el-select filterable v-model="queryParams.machineId">
          <el-option
            v-for="item in workbranchList"
            :label="item.machineName"
            :key="item.pid"
            :value="item.pid"
          ></el-option>
        </el-select>
        <!-- <el-input
          v-model.trim="queryParams.machineId"
          style="width: 200px"
          placeholder="请输入机台"
          @keyup.enter="handleQuery"
        /> -->
      </el-form-item>
      <el-form-item label="出版编码" prop="pubNo">
        <el-input
          v-model.trim="queryParams.pubNo"
          style="width: 200px"
          placeholder="请输入出版编码"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="生产单编码" prop="produceNo">
        <el-input
          v-model.trim="queryParams.produceNo"
          style="width: 200px"
          placeholder="请输入生产单编码"
          @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-button type="primary" plain @click="handlePointsPrint"
        >分配印刷</el-button
      >
      <!-- <right-toolbar
              v-model:showSearch="showSearch"
              @queryTable="getList"
              :columns="columns"
          ></right-toolbar> -->
    </el-row>
    <div>
      <el-table v-loading="loading" border :data="dataList">
        <el-table-column label="序号" width="60" type="index" align="center" />
        <el-table-column
          label="状态"
          min-width="100"
          align="center"
          :show-overflow-tooltip="true"
          prop="status"
          v-if="columns[0].visible"
        >
          <template v-slot="{ row }">
            <dict-tag :options="ems_order_status" :value="row.status" />
          </template>
        </el-table-column>
        <el-table-column
          label="生产单编码"
          min-width="150"
          align="center"
          :show-overflow-tooltip="true"
          prop="produceNo"
          v-if="columns[1].visible"
        >
          <template v-slot="{ row }">
            <el-button type="primary" @click="handleView(row)" text>
              {{ row.produceNo }}
            </el-button>
          </template>
        </el-table-column>
        <el-table-column
          label="书刊名称"
          align="center"
          min-width="280"
          prop="shipmentType"
          v-if="columns[2].visible"
        >
        </el-table-column>
        <el-table-column
          label="出版编码"
          min-width="100"
          align="center"
          prop="pubNo"
          v-if="columns[3].visible"
        ></el-table-column>
        <el-table-column
          label="订单数量"
          min-width="100"
          align="center"
          prop="orderCount"
          v-if="columns[3].visible"
        >
          <template v-slot="{ row }">
            <span v-if="row.type == 2">
              {{ row.orderCount }}
            </span>
            <span
              v-else
              v-for="(item, index) in row.produceCount?.split(',')"
              :key="index"
            >
              {{ item }}
            </span>
          </template>
        </el-table-column>
        <el-table-column
          label="下单日期"
          min-width="100"
          align="center"
          prop="submitDate"
          v-if="columns[3].visible"
        >
          <template v-slot="{ row }">
            {{ row.submitDate ? row.submitDate.split("T")[0] : "" }}
          </template>
        </el-table-column>
        <el-table-column
          label="加急交货日期"
          style="color: red"
          min-width="120"
          align="center"
          prop="urgentDate"
        >
          <template v-slot="{ row }">
            <span style="color: red">
              {{ parseTime(row.urgentDate) }}
            </span>
          </template>
        </el-table-column>
        <el-table-column
          label="类型"
          min-width="100"
          align="center"
          prop="type"
          v-if="columns[3].visible"
        >
          <template v-slot="{ row }">
            {{ row.type == 1 ? "封面" : "内文" }}
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          width="120"
          fixed="right"
          class-name="small-padding fixed-width"
        >
          <template #default="scope">
            <!-- <el-button link type="primary" v-if="scope.row.istake == 0"  >
            </el-button> -->
            <el-button
              link
              type="primary"
              v-if="scope.row.istake == 1"
              @click="handleSubmit(scope.row)"
            >
              取版
            </el-button>

            <el-tooltip content="打印" placement="top">
              <el-button
                link
                type="primary"
                icon="Printer"
                @click="handlePrint(scope.row)"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
    <printview
      :visible="printOpen"
      :printList="printList"
      @cancel="
        () => {
          printOpen = false;
          printList = [];
        }
      "
      :id="mainId"
    ></printview>
  </div>
</template>

<script setup>
import { downLoadBlob } from "@/utils/ruoyi";
import { taskMachine } from "@/api/production/cover";
import { fpPrint } from "@/api/production/waitTakeVersion";
import {
  getMachine,
  getList as getWorkBenchList,
} from "@/api/basicInfo/workbench";
const { proxy } = getCurrentInstance();
const loading = ref(true);
const dataList = ref([]);
const workbranchList = ref([]);
import useUserStore from "@/store/modules/user";
import Printview from "@/components/PrintView";
import { ref } from "@vue/reactivity";
const userInfo = useUserStore();

const showSearch = ref(true);
const total = ref(0);
const router = useRouter();
const printOpen = ref(false);
const mainId = ref("");
const printList = ref([]);
const selectRow = ref([]);

// const permissions = useUserStore().roles;
// if (permissions.includes("cjry")) {

// }
getMachine().then((res) => {
  res.rows.forEach((item) => {
    //item.item.machineId = item.pid;
    item.machineName = item.machineName + "/" + item.workshopName;
  });
  workbranchList.value = res.rows;
  if (res.rows?.length > 0) {
    queryParams.value.machineId = res.rows[0].pid;
  } else {
    getWorkBenchList({ pageNum: 1, pageSize: 100, type: "2" }).then((resp) => {
      resp.rows.forEach((item) => {
        //item.machineId = item.pid;
        item.machineName = item.machineName + "/" + item.workshopName;
      });
      workbranchList.value = resp.rows;
    });
  }
  getList();
});

// 列显隐信息
const columns = ref([
  { key: 0, label: `状态`, visible: true },
  { key: 1, label: `取版编码`, visible: true },
  { key: 2, label: `取版日期`, visible: true },
  { key: 3, label: `取版人`, visible: true },
  { key: 4, label: `备注`, visible: true },
  { key: 5, label: `创建人`, visible: true },
  { key: 6, label: `创建时间`, visible: true },
]);

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
  },
});
const { queryParams } = toRefs(data);
const { ems_order_status } = proxy.useDict("ems_order_status");

const getList = () => {
  loading.value = true;
  // queryParams.value.machineId = "5bfd2e94fa6d44b3bc4ff2ce4287346b";
  taskMachine(queryParams.value)
    .then((res) => {
      loading.value = false;
      dataList.value = res.rows;
      total.value = res.total;
    })
    .catch((err) => {
      loading.value = false;
    });
};
const handlePointsPrint = () => {
  if (!queryParams.value.machineId) {
    return proxy.$modal.msgError("请先选择机台");
  }
  loading.value = true;
  fpPrint({machineId:queryParams.value.machineId})
    .then((res) => {
      loading.value = false;
      getList();
      proxy.$modal.msgSuccess(res.msg);
    })
    .catch((err) => {
      loading.value = false;
    });
};

// 查看
const handleView = (row) => {
  if (row.type == 1) {
    //封面
    router.push({
      path: "/production/cover/coverDetails",
      query: {
        id: row.produceId,
        info: true,
        moduleCode: "waitTakeVersion",
      },
    });
  } else {
    router.push({
      path: "/production/text/textDetails",
      query: {
        id: row.produceId,
        info: true,
        moduleCode: "waitTakeVersion",
      },
    });
  }
};
// 搜索
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  proxy.resetForm("queryRef");
  handleQuery();
};
/** 删除按钮操作 */
function handleDelete(row) {
  proxy.$modal
    .confirm('是否确认删除"' + row.takeCode + '"的单？')
    .then(function () {
      return delTakeVersionInfo(row.id);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}
// 提交
const handleSubmit = (row) => {
  router.push({
    path: "/production/waitTakeVersion/waitTakeDetails",
    query: {
      id: row.produceId,
      machineId: queryParams.value.machineId,
      produceNo: row.produceNo,
    },
  });
  //   proxy.$modal
  //     .confirm('是否确认提交"' + row.takeCode + '"的取版单？')
  //     .then(function () {
  //       return editTakeVersion({ status: 2, id: row.id });
  //     })
  //     .then(() => {
  //       getList();
  //       proxy.$modal.msgSuccess("提交成功");
  //     })
  //     .catch(() => {});
};

// getList();

const handlePrint = (row) => {
  printList.value = [row];
  printOpen.value = true;
  mainId.value = row.produceId;
};
</script>

<style lang="scss" scoped></style>
